package com.local.controller.admin;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.cz073586.tools.util.Tools;
import com.local.entity.vo.MemberFinanceVO;
import com.local.entity.vo.WorkTransationVo;
import com.local.model.LocalFinanceWork;
import com.local.model.LocalMemberFinance;
import com.local.model.LocalRotaUser;
import com.local.model.WorkTransition;
import com.local.page.LocalMemberFinancePage;
import com.local.page.WorkTransitionPageInfo;
import com.local.service.face.LocalMemberFinanceService;
import com.local.service.face.LocalRotaUserService;
import com.local.service.face.LocalWorkTransService;
import com.qianhai.common.constants.CommonContans;
import com.qianhai.common.controller.BaseController;
import com.qianhai.common.entity.LoginUserInfo;

import common.cz0735.coustomer.model.CoustomerInfo;
import common.cz0735.coustomer.service.face.CoustomerInfoService;

/**
 * 
 * 类描述： 工作交接
 * 
 * @ClassName: LocalIndexWorkTransController
 * @author Administrator
 * @date 2015年10月3日 下午1:06:02
 */
@RestController
@RequestMapping(value = "/admin/workTrans", method = RequestMethod.POST)
public class LocalWorkTransController extends BaseController
{
	@Autowired
	private LocalWorkTransService localWorkTransService;
	
	@Autowired
	private CoustomerInfoService coustomerInfoService;
	
	@Autowired
	private LocalRotaUserService localRotaUserService;
	
	@Autowired
	private LocalMemberFinanceService localMemberFinanceService;
	
	
	/**
	 * 
	 * 方法描述： 显示当前岗亭下的上班信息.
	 * 
	 * @Title: showWorkTrans
	 * @param @param workTransition
	 * @param @return
	 * @return ModelMap
	 * @user lyf 2015年11月20日
	 * @throws
	 */
	@RequestMapping(value = "/showWorkTrans.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	public ModelMap showWorkTrans(@RequestBody WorkTransition workTransition)
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
			if (Tools.isEmpty(this.getLoginUser()))
			{
				new Throwable("50000-00001-00003");
			}
			
			workTransition.setWorkPeopleId(this.getLoginUser().getUuid());
			
			WorkTransition worktrans = localWorkTransService
					.showWorkTrans(workTransition);
			
			if (Tools.isEmpty(worktrans))
			{
				worktrans = workTransition;
			}
			
			if (null != worktrans)
			{
				workTransition.setWorkPeopleId(worktrans.getWorkPeopleId());
				worktrans.setWorkPeopleName(this
						.getLoginUserRealName(worktrans));
			}
			
			resultMap.put(CommonContans.STRUTS_DATA, worktrans);
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	
	@RequestMapping(value = "/queryRotaTime.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	public ModelMap queryRotaTime()
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
			Integer rotaTime = -1;
			LocalRotaUser localRotaUser = localRotaUserService.queryStationByUser(this.getLoginUser().getUuid());
			if(!Tools.isEmpty(localRotaUser)){
				String[] startTimes = localRotaUser.getStartTime().split(":");
				String[] endTimes = localRotaUser.getEndTime().split(":");
				Integer hours = Integer.parseInt(endTimes[0])-Integer.parseInt(startTimes[0]);
				Integer minutes = Integer.parseInt(endTimes[1])-Integer.parseInt(startTimes[1]);
				if(hours<0){
					hours +=24;
				}
				if(minutes<0){
					hours -=1;
					minutes +=60;
				}
				rotaTime = hours*3600+minutes*60;
			}
			resultMap.put(CommonContans.STRUTS_DATA, rotaTime);
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	
	
	/**
	 * 
	 * 方法描述：添加交班记录
	 * 
	 * @Title: addSentryBox
	 * @param @param localStation
	 * @param @return
	 * @return ModelMap
	 * @user Administrator 2015年4月7日
	 * @throws
	 */
	@RequestMapping(value = "/addWorkTrans.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	public ModelMap addWorkTrans(@RequestBody WorkTransition workTransition)
	{
		ModelMap resultMap = new ModelMap();
		try
		{
			workTransition.setWorkPeopleId(this.getLoginUser().getUuid());
			
			localWorkTransService.addLocalWorkTrans(workTransition);
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	/**
	 * 
	 * 方法描述： 修改上班信息
	 * 
	 * @Title: modifyStation
	 * @param @param workTransition
	 * @param @return
	 * @return ModelMap
	 * @user lyf 2015年11月20日
	 * @throws
	 */
	@RequestMapping(value = "/modifyWorkTrans.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	public ModelMap modifyStation(@RequestBody WorkTransition workTransition)
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
			if (!workTransition.getWorkPeopleId().equals(
					this.getLoginUser().getUuid()))
			{
				
				throw new Throwable("50000-00001-00037");
			}
			localWorkTransService.modifyLocalWorkTrans(workTransition);
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	/**
	 * 
	 * 方法描述：确定交班
	 * 
	 * @Title: confrimWorkStation
	 * @param @param workTransition
	 * @param @return
	 * @return ModelMap
	 * @user lyf 2015年11月20日
	 * @throws
	 */
	@RequestMapping(value = "/confrimWorkStation.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	public ModelMap confrimWorkStation(
			@RequestBody WorkTransition workTransition)
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
			
			workTransition.setStatus(3);// 财务确认交班
			localWorkTransService.modifyLocalWorkTrans(workTransition);
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	/**
	 * 
	 * 方法描述： 转换登录用户名
	 * 
	 * @Title: getLoginUserRealName
	 * @param @param worktrans
	 * @param @return
	 * @return String
	 * @user lyf 2015年11月20日
	 * @throws
	 */
	private String getLoginUserRealName(WorkTransition worktrans)
	{
		
		CoustomerInfo info = coustomerInfoService
				.getCustmoerByLoginId(worktrans.getWorkPeopleId());
		if (!Tools.isEmpty(info))
		{
			return info.getName();
		}
		
		return this.getLoginUser().getLoginName();
	}
	
	/**
	 * 
	 * 方法描述： 分页查询
	 * 
	 * @Title: findAllLocalWorkTrans
	 * @param @param pageInfo
	 * @param @return
	 * @return ModelMap
	 * @user lyf 2015年11月20日
	 * @throws
	 */
	@RequestMapping(value = "/findAllLocalWorkTrans.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	public ModelMap findAllLocalWorkTrans(
			@RequestBody WorkTransitionPageInfo pageInfo)
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
			
			localWorkTransService.findAllLocalWorkTransByQueryForm(pageInfo);
			
			resultMap.put(CommonContans.STRUTS_DATA, pageInfo);
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	/**
	 * 
	 * 方法描述： 统计查看，下班待处理
	 * 
	 * @Title: findWorkTransitonByuuid
	 * @param @param uuid
	 * @param @return
	 * @return ModelMap
	 * @user lyf 2015年11月20日
	 * @throws
	 */
	@RequestMapping(value = "/findWorkTransitonByuuid.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	public ModelMap findWorkTransitonByuuid(String uuid)
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
			
			WorkTransationVo info = localWorkTransService.findWorkTransitonByuuid(uuid);
			
			resultMap.put(CommonContans.STRUTS_DATA, info);
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	
	/**
	 * 
	 * 方法描述：财务确认交班
	 * @Title: confirmWorkTransiton 
	 * @param @param localFinanceWork
	 * @param @return
	 * @return ModelMap
	 * @user Administrator
	 * 2016年4月21日
	 * @throws
	 */
	@RequestMapping(value = "/confirmWorkTransiton.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	public ModelMap confirmWorkTransiton(@RequestBody LocalFinanceWork localFinanceWork)
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
			LoginUserInfo loginUserInfo = this.getLoginUser();
			if(!Tools.isEmpty(loginUserInfo)){
				localFinanceWork.setCreateUserUuid(loginUserInfo.getUuid());
				localFinanceWork.setCreateUserName(loginUserInfo.getLoginName());
			}
			localWorkTransService.confirmWorkTransiton(localFinanceWork);
			
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	/**
	 * 
	 * 方法描述：查看财务确认详情信息
	 * @Title: queryFinanceByWorkId 
	 * @param @param workUuid
	 * @param @return
	 * @return ModelMap
	 * @user Administrator
	 * 2016年4月21日
	 * @throws
	 */
	@RequestMapping(value = "/queryFinanceByWorkId.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	public ModelMap queryFinanceByWorkId(String workUuid)
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
			LoginUserInfo loginUserInfo = this.getLoginUser();
			LocalFinanceWork localFinanceWork=localWorkTransService.queryFinanceByWorkId(workUuid);
			
			resultMap.put(CommonContans.STRUTS_DATA, localFinanceWork);
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	
	@RequestMapping(value = "/queryMemberFinance.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	public ModelMap queryMemberFinance(@RequestBody LocalMemberFinancePage<LocalMemberFinance> page)
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
			List<LocalMemberFinance> list = localMemberFinanceService.queryMemberFinanceByPage(page);
			page.setData(list);
			resultMap.put(CommonContans.STRUTS_DATA, page);
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	
	@RequestMapping(value = "/queryMemberSumFinance.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	public ModelMap queryMemberSumFinance(@RequestBody LocalMemberFinancePage<LocalMemberFinance> page)
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
			MemberFinanceVO memberFinanceVO = localMemberFinanceService.queryMemberSumFinance(page.getStartTime(),page.getEndTime());
			resultMap.put(CommonContans.STRUTS_DATA, memberFinanceVO);
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	
	
}
